package com.oocl.ajax.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.ajax.pojo.Area;

/**
 * @description： Area 与 json 互相转换
 * @author XIEMI2
 * @date Jul 28, 2017 2:33:18 PM
 * @version 1.0
 */
public class JsonUtil {
	private static ObjectMapper mapper = new ObjectMapper();

	public static String parserToString(Area Area) throws JsonProcessingException {
		String jsonStr = null;
		jsonStr = mapper.writeValueAsString(Area);
		

		return jsonStr;
	}
	
	
	public static String parsetToStrings(List<Area> area) throws JsonProcessingException{
		return mapper.writeValueAsString(area);
	}
	

	public static Area parserToDTO(String json) throws JsonParseException, JsonMappingException, IOException {
		Area Area = null;
		Area = mapper.readValue(json, Area.class);
		return Area;
	}

	public static List<Area> parserToDTOs(String json) throws JsonParseException, JsonMappingException, IOException{
		List<Area> Areas = new ArrayList<Area>();
		JavaType javaType = getCollectionType(ArrayList.class, Area.class); 
		Areas = mapper.readValue(json, javaType);
		return Areas;
	}

	
	 /**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
	 public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {   
	     return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);   
	 }
}
